import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

enum AnimationType {
  rightToLeft, // 从右到左
  leftToRight, // 从左到右
  bottomToTop, // 从下到上
  topToBottom, // 从上到下
  fade, // 淡入淡出
  scale, // 缩放
  rotation // 旋转
}

class RouteAnimation {
  void animateRoute(
    BuildContext context,
    Widget page, {
    AnimationType type = AnimationType.rightToLeft,
    Duration duration = const Duration(milliseconds: 300),
    Curve curve = Curves.easeInOutCubic,
  }) {
    Navigator.push(
      context,
      PageRouteBuilder(
        settings: RouteSettings(name: ''),
        pageBuilder: (_, __, ___) => page,
        transitionsBuilder: (_, animation, secondaryAnimation, child) {
          // 确保在动画完成后释放资源
          animation.addStatusListener((status) {
            if (status == AnimationStatus.completed ||
                status == AnimationStatus.dismissed) {
              SystemChrome.setPreferredOrientations([]); // 重置屏幕方向限制
            }
          });

          // 使用更高效的动画构建方式
          switch (type) {
            case AnimationType.rightToLeft:
              return _buildSlideTransition(
                  animation, child, const Offset(1.0, 0.0), Offset.zero, curve);
            case AnimationType.leftToRight:
              return _buildSlideTransition(animation, child,
                  const Offset(-1.0, 0.0), Offset.zero, curve);
            case AnimationType.bottomToTop:
              return _buildSlideTransition(
                  animation, child, const Offset(0.0, 1.0), Offset.zero, curve);
            case AnimationType.topToBottom:
              return _buildSlideTransition(animation, child,
                  const Offset(0.0, -1.0), Offset.zero, curve);
            case AnimationType.fade:
              return FadeTransition(
                opacity: animation,
                child: child,
              );
            case AnimationType.scale:
              return ScaleTransition(
                scale: Tween<double>(begin: 0.95, end: 1.0)
                    .animate(CurvedAnimation(
                  parent: animation,
                  curve: curve,
                )),
                child: child,
              );
            case AnimationType.rotation:
              return RotationTransition(
                turns:
                    Tween<double>(begin: 0.0, end: 1.0).animate(CurvedAnimation(
                  parent: animation,
                  curve: Interval(0.0, 0.5, curve: curve),
                )),
                child: FadeTransition(
                  opacity: animation,
                  child: child,
                ),
              );
          }
        },
        transitionDuration: duration,
        reverseTransitionDuration: duration,
        fullscreenDialog: false,
        maintainState: true,
      ),
    );
  }

  Widget _buildSlideTransition(Animation<double> animation, Widget child,
      Offset begin, Offset end, Curve curve) {
    return SlideTransition(
      position: Tween<Offset>(begin: begin, end: end).animate(CurvedAnimation(
        parent: animation,
        curve: curve,
      )),
      child: FadeTransition(
        opacity: animation,
        child: child,
      ),
    );
  }
}
